package cn.shiro.shiroservice.auth.service.impl;

import cn.shiro.shiroservice.auth.pojo.vo.out.UserInfoOutVo;
import cn.shiro.shiroservice.auth.service.AccessCacheManager;
import cn.shiro.shiroservice.common.constant.ManaPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * &#064;Time 2024 一月 星期二 18:15
 *
 * @author ShangGuan
 */
public class TokenCacheManger implements AccessCacheManager<UserInfoOutVo> {

    private final Logger  log = LoggerFactory.getLogger(TokenCacheManger.class);

    /**
     * 名称 保证key的唯一性
     */
    private final String name = ManaPoint.CLASS_CACHE;

    private final RedisTemplate<String,Object> redisTemplate;
    public String getName() {
        return name;
    }
    public TokenCacheManger(RedisTemplate<String,Object> redisTemplate){
        this.redisTemplate=redisTemplate;
    }

    @Override
    public boolean save(String key, UserInfoOutVo value) {
        try {
            redisTemplate.opsForValue().set(key+name,value);
            return true;
        }catch (Exception exception){
            log.error("缓存保存发生异常:{}",exception.getMessage());
            return false;
        }
    }

    @Override
    public UserInfoOutVo get(String key) {
        UserInfoOutVo result = (UserInfoOutVo) redisTemplate.opsForValue().get(key+name);
        if(result!=null){
            log.info("缓存存在相对应的值:{}",result);
        }
        return result;
    }

    @Override
    public boolean delete(String key) {
        return Boolean.TRUE.equals(redisTemplate.delete(key + name));
    }

    @Override
    public void clearCache() {

    }
}
